/**
 * 
 */
package com.flying.dcss.dcmanager.action;

import java.io.PrintWriter;

import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;
import org.json.JSONObject;

import com.flying.dcss.dcmanager.bo.impl.DcssDCInfo;
import com.flying.dcss.dcmanager.service.impl.DCInfoService;
import com.flying.dcss.userpermissions.bo.impl.DcssUser;
import com.flying.dcss.userpermissions.bo.impl.DcssUserInfo;
import com.flying.dcss.userpermissions.service.impl.UserInfoService;
import com.flying.dcss.userpermissions.service.impl.UserService;
import com.opensymphony.xwork2.ActionSupport;

// TODO: Auto-generated Javadoc
/**
 * The Class SelectDCManagerAutoAction.
 *
 * @author EvanHong
 */
public class SelectDCManagerAutoAction extends ActionSupport {
	/** The user service. */
	private UserService userService;

	/** The user info service. */
	private UserInfoService userInfoService;

	/** The dc info service. */
	private DCInfoService dcInfoService;

	/**
	 * Sets the user service.
	 * 
	 * @param userService
	 *            the new user service
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * Sets the user info service.
	 * 
	 * @param userInfoService
	 *            the new user info service
	 */
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	/**
	 * Sets the dc info service.
	 *
	 * @param dcInfoService the new dc info service
	 */
	public void setdcInfoService(DCInfoService dcInfoService) {
		this.dcInfoService = dcInfoService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		String dcId = ServletActionContext.getRequest().getParameter("dcId");

		if (dcId == null) {
			return null;
		}

		// Get manager list.
		DcssDCInfo dcInfo = dcInfoService.getById(dcId);

		JSONArray jsonArray = new JSONArray();

		DcssUserInfo userInfo = userInfoService.getUserInfoByUserId(dcInfo
				.getManagerId());
		DcssUser user = userService.getUserById(dcInfo.getManagerId());

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("email", user.getEmail());
		jsonObject.put("realName", userInfo.getRealName());
		jsonObject.put("empoyeeNum", userInfo.getEmpoyeeNum());
		jsonArray.put(jsonObject);

		ServletActionContext.getResponse().setContentType(
				"text/html;charset=UTF-8");
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		out.print(jsonArray);
		out.flush();
		out.close();

		return null;
	}

}
